<template>
  <div>
    <el-form :model="formData" ref="form" :rules="rules">
      <el-form-item label="本单位历史审核意见" v-loading="loading">
        <el-table :data="formData.list" border stripe height="300">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="name" label="审核单位">
          </el-table-column>
          <el-table-column prop="message" label="审核信息">
          </el-table-column>
          <el-table-column prop="create_date" label="审核时间" width="100">
            <template slot-scope="{row}">
              {{$util.formatDate(row.create_date)}}
            </template>
          </el-table-column>
          <el-table-column label="审核状态" width="80">
            <template slot-scope="{row}">
              {{row.check_status | f_checkStatus}}
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item label="保障部门审核意见">
        <el-input v-model="formData.ensure_check_msg" size="normal" type="textarea" :rows="4" clearable></el-input>
      </el-form-item>
    </el-form>
    <div class="center">
      <el-button type="primary" size="default" @click="cancel" :loading="saveLoading">取消</el-button>
      <el-button type="danger" size="default" @click="reject" :loading="saveLoading">驳回</el-button>
      <el-button type="success" size="default" @click="submit" :loading="saveLoading">通过审核</el-button>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { f_checkStatus } from '@/utils/filters.js'
export default {
  name: 'Verify',
  components: {

  },
  props: {
    verifyParams: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      loading: false,
      saveLoading: false,
      formData: {},
      rules: {},
    }
  },
  created () {

  },
  mounted () {
    if (this.verifyParams.id) {
      this.init()
    }
  },
  methods: {
    async init () {
      this.loading = true
      const res = await this.$http('verifyLog', {
        id: this.verifyParams.id,
        status: this.verifyParams.status,
      })
      this.formData = res
      this.loading = false
    },
    cancel () {
      this.saveLoading = false
      this.$emit('cancel');
    },
    reject () {
      this.$confirm('是否确认驳回?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.saveLoading = true
        this.reject1()
      }).catch(() => {

      });
    },
    async reject1 () {
      let date = new Date()
      let today = this.$util.formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      let check_end_time = await this.$http('checkDate', {
        today: today,
        num: '1'
      })

      const res = await this.$http('inputProjectUp', {
        id: this.verifyParams.id,
        level: this.verifyParams.status,
        check_status: '2',
        ensure_check_msg: this.formData.ensure_check_msg,
        status: this.verifyParams.status - 1,
        check_end_time: check_end_time,
        log: {
          project_id: this.verifyParams.id,
          company_code: this.user.company_code,
          login_name: this.user.loginName,
          check_status: '2',
          status: this.verifyParams.status,
          target_status: this.verifyParams.status - 1,
          message: this.formData.ensure_check_msg,
        }
      })
      this.saveLoading = false
      if (res.errcode === 4000) {
        this.$tips.declareUp(res.errmsg)
      } else if (res.errcode === 9999) {
        this.$tips.error(res.errmsg)
      } else {
        this.$tips.success(res.errmsg)
      }
      this.$emit('cancel');
      this.$emit('init');
    },
    submit () {
      this.$confirm('是否确认上报?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.saveLoading = true
        this.submit1()
      }).catch(() => {

      });
    },
    async submit1 () {
      let target_status
      const log = await this.$http('inputDisLog', {
        project_id: this.verifyParams.id
      })
      for (let item of log.list) {
        /*  先判断是否有 4,51,52驳回
          4,52驳回直接回传4
          51驳回回传51
        */
        if (item.check_status === '2') {
          if (item.status === '52') {
            target_status = '4'
            break
          } else if (item.status === '4') {
            target_status = '4'
            break
          } else if (item.status === '51') {
            target_status = '51'
            break
          }
        } else {
          target_status = '3'
          break
        }
      }
      // 获取系统时间
      let check_end_time = ''
      let date = new Date()
      let today = this.$util.formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      check_end_time = await this.$http('checkDate', {
        today: today,
        num: target_status.slice(0, 1)
      })
      const res = await this.$http('inputProjectUp', {
        id: this.verifyParams.id,
        level: this.verifyParams.status,
        check_status: '1',
        ensure_check_msg: this.formData.ensure_check_msg,
        status: target_status,
        check_end_time: check_end_time,
        // company_code: item.company_code,
        log: {
          project_id: this.verifyParams.id,
          company_code: this.user.company_code,
          login_name: this.user.loginName,
          check_status: '1',
          status: this.verifyParams.status,
          target_status: target_status,
          message: this.formData.message,
        }
      })
      this.saveLoading = false
      if (res.errcode === 4000) {
        this.$tips.declareUp(res.errmsg)
      } else if (res.errcode === 9999) {
        this.$tips.error(res.errmsg)
      } else {
        this.$tips.success(res.errmsg)
      }

      this.$emit('cancel');
      this.$emit('init');
    },
    check () {
      return true
    }
  },
  computed: {
    ...mapGetters([
      'user',
      'user1'
    ])
  },
  filters: {
    f_checkStatus: f_checkStatus
  }
}
</script>
<style lang='scss' scoped>
</style>
